﻿using System;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.FAQ.Controls
{
    public partial class ApproveItem : ApproveItemControl
    {       
        protected override void OnInit(EventArgs e)
        {
            this.ButtonApprove = btnApprove;
            this.ButtonCancel = btnCancel;
            this.Approve += new EventHandler(Event_Approve);
            base.OnInit(e);
        }

        void Event_Approve(object sender, EventArgs e)
        {
            Components.Item item = new Components.Item();
            item.ID = this.ItemID;
            item.ApprovedBy= PortalSecurity.CurrentUser.Identity.Email;
            DateTime now = DateTime.Now;

            item.ApprovedDate = now;

            int h = Convert.ToInt32(ddlPublishedHour.SelectedValue);
            int m = Convert.ToInt32(ddlPublishedMinute.SelectedValue);
            DateTime d = Convert.ToDateTime(txtPublishedDate.Text);
            
            // Chọn tức thì.
            if (ddlPublishedDateOption.SelectedValue == "0")
            {
                item.PublishedDate = item.ApprovedDate;
            }
            else
            {
                item.PublishedDate = new DateTime(d.Year, d.Month, d.Day, h, m, 0);
            }
            h = Convert.ToInt32(ddlExpiredHour.SelectedValue);
            m = Convert.ToInt32(ddlExpiredMinute.SelectedValue);
            d = Convert.ToDateTime(txtExpiredDate.Text);

            if (ddlExpiredDateOption.SelectedValue == "0")
            {
                item.ExpiredDate = DateTime.MaxValue;
            }
            else
            {
                item.ExpiredDate = new DateTime(d.Year, d.Month, d.Day, h, m, 0);
            }

            if (item.PublishedDate < item.ApprovedDate) item.PublishedDate = item.ApprovedDate;

            int result = item.Approve();
            string url = HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, this.ItemID, ModulePages.Item);
            Response.Redirect(url);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (this.ItemID > 0)
                {

                    for (int d = 0; d < 24; d++)
                    {
                        ddlPublishedHour.Items.Add(new ListItem(d.ToString(), d.ToString()));
                        ddlExpiredHour.Items.Add(new ListItem(d.ToString(), d.ToString()));
                    }

                    for (int m = 0; m < 60; m++)
                    {
                        ddlPublishedMinute.Items.Add(new ListItem(m.ToString(), m.ToString()));
                        ddlExpiredMinute.Items.Add(new ListItem(m.ToString(), m.ToString()));
                    }
                    DateTime now = DateTime.Now;
                    ddlPublishedHour.SelectedValue = now.Hour.ToString();
                    ddlPublishedMinute.SelectedValue = now.Minute.ToString();
                    txtPublishedDate.Text = now.ToShortDateString();
                    
                    DateTime maxDate = DateTime.MaxValue;
                    ddlExpiredHour.SelectedValue = maxDate.Hour.ToString();
                    ddlExpiredMinute.SelectedValue = maxDate.Minute.ToString();
                    txtExpiredDate.Text = maxDate.ToShortDateString();

                    Components.Item item = Components.Item.Load(this.ItemID);
                    if (item != null)
                    {
                        // Tiêu đề.
                        lblTitle.Text = item.Title;

                        // Người trả lời.
                        this.lblNguoiTraLoi.Text = item.AnwserBy;

                        //Nội dung trả lời
                        lblAnswer.Text = item.AnwserContent;
                    }
                }
            }
        }
    }
}